package com.java.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.business.dao.CheckinMapper;
import com.java.business.model.Appointment;
import com.java.business.model.Checkin;
import com.java.business.model.MaintainRecord;
import com.java.business.service.CheckinService;
import com.java.common.model.Result;
import com.java.common.util.JwtUtil;
import com.java.manage.model.*;
import com.java.manage.service.api.*;
import com.java.userservice.model.User;
import com.java.userservice.service.api.UserServiceApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CheckinServiceImpl implements CheckinService {

    @Autowired
    private CheckinMapper checkinMapper;

    @DubboReference
    private UserServiceApi userServiceApi;

    @DubboReference
    private BrandApiService brandApiService;

    @DubboReference
    private ColorApiService colorApiService;

    @DubboReference
    private ModelApiService modelApiService;

    @DubboReference
    private PlateApiService plateApiService;

    @DubboReference
    private CompanyApiService companyApiService;

    @Autowired
    private HttpServletRequest request;

    @Override
    public Result list(Checkin checkin) {
        PageHelper.startPage(checkin.getPageNum(), checkin.getPageSize());

        String token = request.getHeader("Token");
        Integer storeId = (Integer) JwtUtil.getClaims(token).get("storeId");
        checkin.setStoreId(storeId);

        List<Checkin> list = checkinMapper.list(checkin);

        // 用户表
        List<Integer> userCollect = list.stream().map(Checkin::getUserid).collect(Collectors.toList());
        List<User> users = userServiceApi.listByIds(userCollect);
        Map<Integer, User> userMap = users.stream().collect(Collectors.toMap(user -> user.getId(), user -> user));

        // 品牌表
        List<Integer> brandCollect = list.stream().map(Checkin::getBrandId).collect(Collectors.toList());
        List<Brand> brands = brandApiService.listByIds(brandCollect);
        Map<Integer, Brand> brandMap = brands.stream().collect(Collectors.toMap(brand -> brand.getId(), brand -> brand));

        // 颜色表
        List<Integer> collectCollect = list.stream().map(Checkin::getColorId).collect(Collectors.toList());
        List<Color> colors = colorApiService.listByIds(collectCollect);
        Map<Integer, Color> colorMap = colors.stream().collect(Collectors.toMap(color -> color.getId(), color -> color));

        // 车型表
        List<Integer> modelCollect = list.stream().map(Checkin::getModelId).collect(Collectors.toList());
        List<Model> models = modelApiService.listByIds(modelCollect);
        Map<Integer, Model> modelMap = models.stream().collect(Collectors.toMap(model -> model.getId(), model -> model));

        // 车牌表
        List<Integer> plateCollect = list.stream().map(Checkin::getPlateId).collect(Collectors.toList());
        List<Licenseplate> licenseplates = plateApiService.listByIds(plateCollect);
        Map<Integer, Licenseplate> licenseplateMap = licenseplates.stream().collect(Collectors.toMap(plate -> plate.getId(), plate -> plate));

        // 门店表
        List<Integer> storeCollect = list.stream().map(Checkin::getStoreId).collect(Collectors.toList());
        List<Company> companies = companyApiService.listByIds(storeCollect);
        Map<Integer, Company> companyMap = companies.stream().collect(Collectors.toMap(store -> store.getId(), store -> store));


        list.stream().forEach(app -> {
            app.setStore(companyMap.get(app.getStoreId()));
            app.setUser(userMap.get(app.getUserid()));
            app.setBrand(brandMap.get(app.getBrandId()));
            app.setColor(colorMap.get(app.getColorId()));
            app.setModel(modelMap.get(app.getModelId()));
            app.setPlate(licenseplateMap.get(app.getPlateId()));
        });

        PageInfo<Checkin> pageInfo = new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());

        return Result.success(map);
    }

    @Override
    public Result save(Checkin checkin) {
        checkinMapper.save(checkin);
        return Result.success();
    }

    @Override
    public Result modify(Checkin checkin) {
        checkinMapper.modify(checkin);
        return Result.success();
    }

    @Override
    public Result remove(int[] ids) {
        for (int id : ids) {
            checkinMapper.remove(id);
        }
        return Result.success();
    }

    @Override
    public Result get(int id) {
        checkinMapper.get(id);
        return Result.success();
    }

    @Override
    public Checkin get(Integer id) {
        Checkin checkin = checkinMapper.get(id);
        Licenseplate licenseplate = plateApiService.listAll(checkin.getPlateId());
        checkin.setPlate(licenseplate);
        return checkin;
    }
}
